//
//  ViewController.m
//  DemoIflytek
//
//  Created by 唐可雄 on 16/7/10.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "ViewController.h"

#import <iflyMSC/IFlyMSC.h>

@interface ViewController ()<IFlyRecognizerViewDelegate>

@property (strong, nonatomic) IFlyRecognizerView *iflyRecognizerView;

@end

@implementation ViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.resultLabel.numberOfLines = 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Setter and Getter

- (IFlyRecognizerView *)iflyRecognizerView {
    if (_iflyRecognizerView == nil) {
        _iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
        _iflyRecognizerView.delegate = self;
        //asr_audio_path保存录音文件名,默认目录是documents
        [_iflyRecognizerView setParameter: @"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
        //设置返回的数据格式为默认plain
        [_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
        [_iflyRecognizerView setParameter:@"0" forKey:[IFlySpeechConstant ASR_PTT]];
    }
    
    return _iflyRecognizerView;
}

#pragma mark - IBAction

/// 点击说话
- (IBAction)sayBtnDidClick:(id)sender {
    [IFlySpeechUtility createUtility:[NSString stringWithFormat:@"appid=%@", @"578234be"]];
    
    [self.iflyRecognizerView start];
}

#pragma mark - IFlyRecognizerViewDelegate

- (void)onError:(IFlySpeechError *)error {
    NSLog(@"%@", error);
}

- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast {
    NSMutableString *result = [NSMutableString new];
    NSDictionary *dic = [resultArray objectAtIndex:0];
    for (NSString *key in dic) {
        [result appendFormat:@"%@",key];
    }
    
    self.resultLabel.text = [NSString stringWithFormat:@"%@\n%@", self.resultLabel.text, result];
}

@end
